#include <iostream>
#include <unordered_map>
#include <vector>

//帧结构体
struct Frame 
{
    int sourceAddress;
    int sourcePort;
    int destinationAddress;
};

//模拟网桥
class Bridge 
{
    private:
        std::unordered_map<int, int> macTable; 

    public:
        int processFrame(const Frame& frame) 
        {
            macTable[frame.sourceAddress] = frame.sourcePort;   // 学习源地址和源端口的映射

            auto it = macTable.find(frame.destinationAddress);
            if (it != macTable.end()) 
            {
                return it->second;
            } 
            else if (frame.destinationAddress == 0xF) 
            {
                return (frame.sourcePort == 1) ? 2 : 1;
            }
            return -1; 
        }

        // 打印 MAC 地址表
        void printMacTable() const 
        {
            std::cout << "MAC 地址表:" << std::endl;
            for (const auto& entry : macTable) 
            {
                std::cout << "MAC 地址: 0x" << std::hex << entry.first << ", 端口: " << entry.second << std::endl;
            }
        }
};

int main() 
{
    Bridge bridge;
    std::vector<Frame> frames = 
    {
        {0x1, 1, 0x2},
        {0x2, 2, 0x1},
        {0x3, 1, 0xF}
    };

    for (const auto& frame : frames) 
    {
        int destinationPort = bridge.processFrame(frame);
        std::cout << "源地址: 0x" << std::hex << frame.sourceAddress
                  << ", 源端口: " << frame.sourcePort
                  << ", 目的地址: 0x" << frame.destinationAddress
                  << ", 目的端口: ";
        if (destinationPort == -1) 
        {
            std::cout << "泛洪";
        } 
        else 
        {
            std::cout << destinationPort;
        }
        std::cout << std::endl;
    }

    bridge.printMacTable();

    return 0;
}    